package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.ProductDir;
import cn.wolfcode.trip.base.query.ProductDirQueryObject;
import cn.wolfcode.trip.base.service.IProductDirService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Copyright: Copyright (c) 2019 Super520
 * 描述：商品类型控制器
 *
 * @package: cn.wolfcode.trip.admin.controller
 * @author: Patch
 * @date: 2019/5/31 17:22
 * 总结:
 */
@Controller
@RequestMapping("productDir")
public class ProductDirController {
    @Autowired
    private IProductDirService dirService;
    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") ProductDirQueryObject qo) {
        model.addAttribute("pageInfo", dirService.query(qo));
        // 查询所有的商品父类型
        model.addAttribute("parents", dirService.listParentAll());
        return "shop/dir/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(ProductDir dir) {
        JsonResult result = new JsonResult();
        try {
            dirService.saveOrUpdate(dir);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("亲，操作失败，请重试");
        }
        return result;
    }
    /**
     * 二级联动查询
     *  根据父分类查询子分类
     */
    @RequestMapping("listByParentId")
    @ResponseBody
    public List<ProductDir> listByParentId(Long parentId){
        return dirService.listByParentId(parentId);
    }

}
